package com.lei.lucene;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.Lock;
import org.apache.lucene.store.LockFactory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;

/**
 * 索引的测试类
 * 
 * @author Spinacia
 * 
 */
public class IndexTest {

	public IndexWriter getIndexWriter() throws Exception {

		// 创建一个目录存放索引文件
		Directory dir = FSDirectory.open(new File("F:\\JAVA\\luceneIndexs"));

		// 文件分析器
		Analyzer analyzer = new SimpleAnalyzer();

		// 配置文件
		IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST, analyzer);

		// 创建一个写索引对象
		IndexWriter indexWriter = new IndexWriter(dir, conf);

		return indexWriter;
	}

	// 删除索引
	@Test
	public void deleteIndexs() throws Exception {

		// 得到一个索引写对象
		IndexWriter indexWriter = this.getIndexWriter();

		// 删除所有索引
		indexWriter.deleteAll();

		// 关闭资源
		try {
			indexWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 删除指定索引
	@Test
	public void deletePointIndexs() throws Exception {

		// 得到一个索引写对象
		IndexWriter indexWriter = this.getIndexWriter();

		Query query = new TermQuery(new Term("filename", "apache"));
		// 删除指定索引
		//indexWriter.deleteDocuments(new Term("filecontent", "spring"));
		indexWriter.deleteDocuments(query);

		// 关闭资源
		try {
			indexWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//修改索引
	@Test
	public void modIndexs() throws Exception {
		
		// 得到一个索引写对象
		IndexWriter indexWriter = this.getIndexWriter(); 
		
		// 构建一个文档对象
		Document doc = new Document();
		
		// 模拟数据
		doc.add(new TextField("newfilename", "测试用文件名", Store.YES));
		doc.add(new TextField("newfilecontent", "测试用内容", Store.YES));
		
		// 修改索引
		indexWriter.updateDocument(new Term("filename", "spring"), doc);
		
		// 关闭资源
		try {
			indexWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	//创建索引库
	@Test
	public void createIndex2() throws Exception {
		
		// 创建一个目录存放索引文件
		File dir = new File("F:\\indexs");
		if (!dir.exists()) {
			dir.mkdirs();
		}
		Directory directory = FSDirectory.open(dir);
		
		//得到一个索引写对象
		Analyzer analyzer = new IKAnalyzer();
		IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST, analyzer);
		IndexWriter indexWriter = new IndexWriter(directory, conf);
		
		//获得原始文件用io流读入
		File files = new File("F:\\JAVA\\就业班视频\\lucene_day01\\资料\\searchsource");
		for (File file : files.listFiles()) {
			
			//得到文件名
			String name = file.getName();
			
			//得到文件内容
			String content = FileUtils.readFileToString(file);
			
			//得到文件路径
			String path = file.getPath();
			
			//得到文件大小
			Long size = FileUtils.sizeOf(file);
			
			//创建域
			Field nameField = new TextField("filename", name, Store.YES);
			Field contentField = new TextField("filecontent", content, Store.NO);
			Field pathField = new StoredField("filepath", path);
			Field sizeField = new LongField("filesize", size, Store.YES);
			
			//创建文档对象
			Document document = new Document();
			
			//将域添加到文档中
			document.add(nameField);
			document.add(contentField);
			document.add(pathField);
			document.add(sizeField);
			
			//创建索引
			indexWriter.addDocument(document);
		}
		
		//关闭资源
		try {
			indexWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//搜索索引库
	@Test
	public void searchIndex() throws Exception {
		
		//读取索引库
		Directory directory = FSDirectory.open(new File("F:\\indexs"));
		
		//得到一个索引搜索对象
		IndexReader indexReader = DirectoryReader.open(directory);
		IndexSearcher indexSearcher = new IndexSearcher(indexReader);
		
		//通过search对象进行搜索
		Query query = new TermQuery(new Term("filename","apache"));
		TopDocs topDocs = indexSearcher.search(query, 10);
		System.out.println("总计查找到的数据共有:" + topDocs.totalHits);
		for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
			Integer docId = scoreDoc.doc;
			Document document = indexSearcher.doc(docId);
			System.out.println(document.get("filename"));
			System.out.println(document.get("content"));
			System.out.println(document.get("filesize"));
			System.out.println(document.get("filepath"));
		}
		
		//关闭资源
		try {
			indexReader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
